$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'gift/pointsexchange/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id_', width: 50, key: true,hidden:true },
			{ label: '商品名称', name: 'name', index: 'name', width: 80 },
			{ label: '所需积分', name: 'points', index: 'points', width: 60 },
			{ label: '描述', name: 'remark', index: 'remark', width: 120 },
			{ label: '剩余量', name: 'remaining', index: 'remaining', width: 60 },
			{ label: '商品图片', name: 'url', index: 'url', width: 80,hidden:true },
			{ label: '创建时间', name: 'createtime', index: 'createtime', width: 100,formatter:formatSubscribeTime },
			{ label: '创建人', name: 'account', index: 'account_', width: 80,hidden:true },
			{ label: '状态', name: 'flag', index: 'flag', width: 80,formatter:function(value,options,row){
            if(value == "1"){
                return "<span>可用</span>";
            }else if(value == "2"){
                return "<span>禁用</span>";
            }
            } }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		pointsExchange: {},
        exchangeName:''
	},
    computed:{
        createtime: function(){
            var dateStr = "";
            if(this.pointsExchange && this.pointsExchange.createtime){
                var crtTime = new Date(this.pointsExchange.createtime);
                dateStr = dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);
            }else{
                dateStr = dateFtt("yyyy-MM-dd hh:mm:ss",new Date());
            }
            return dateStr;
        },
        account:function(){
            var username = '';
            $.ajax({
                url:baseURL+"sys/user/info?_"+$.now(),
                type:"GET",
                async:false,
                success:function(data){
                    username = data.user.username;
                }
            });
            return username;
        }
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.pointsExchange = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            if(vm.pointsExchange.id == null) {
                var bootstrapValidator = $("#exchangeForm").data('bootstrapValidator');
                bootstrapValidator.validate();
                if (!bootstrapValidator.isValid()) {
                    return;
                }
            }else{
                $('#exchangeForm').data('bootstrapValidator').enableFieldValidators('imagesrc', true);
            }
			var url = vm.pointsExchange.id == null ? "gift/pointsexchange/save" : "gift/pointsexchange/update";
            var formData = new FormData(document.getElementById("exchangeForm"));
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: formData,
                processData: false,  // 告诉jQuery不要去处理发送的数据
                contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "gift/pointsexchange/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "gift/pointsexchange/info/"+id, function(r){
                vm.pointsExchange = r.pointsExchange;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			var postData = $("#jqGrid").jqGrid('getGridParam','postData');
			$.each(postData,(index,value)=>{
			    delete postData[index];
            });
			var params = {
			    "name":vm.exchangeName ==''?undefined:vm.exchangeName
            }

			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:params
            }).trigger("reloadGrid");
		}
	}
});